function heapSort (array){
	function swap (array, i, j){
		array[i] = array[i] + array[j] - ( array[j] = array[i]);
		//console.log('交换的值'+'a[i]='+array[i]+'a[j]='+array[j]);
		//a[i]和a[j]交换
	}
	//最大堆调整
	function maxHeapify (array, index, heapSize){
		var iMax,
			iLeft,
			iRight;
		while (true){
			iMax = index;
			iLeft = 2 * index + 1;
			iRight  = 2 * (index + 1);
			if (iLeft < heapSize && array[index] < array[iLeft]){
				iMax = iLeft;//左子节点比父节点大
			}
			if(iRight < heapSize && array[iMax] < array[iRight]){
				iMax = iRight;//右>父
			}
			if(iMax != index){
				swap(array, iMax, index);//交换值
				index = iMax;
			}
			else{
				break;
			}

		}
		console.log('调整堆',arr);
	}
	function buildMaxHeap (array){
		var i,
			iParent = Math.floor (array.length/2)-1;//从倒数第二层开始（不用从叶子节点开始）
			for (i = iParent; i >=0; i--){
				maxHeapify(array,i,array.length);
			}
	}
	function sort(array){
		buildMaxHeap(array);
		for (var i = array.length -1; i>0; i--){
			swap(array, 0, i);
			maxHeapify(array, 0, i);
		}
		return array;
	}
	return sort(array);
}
var arr = [ 1, 3, 4, 5, 2, 6, 9, 7, 8, 15];
arr = heapSort(arr);
console.log(arr);